﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using SD.DataAccess;

namespace SD.Web.Iteration2.Directorate
{
    public partial class EditDirectorate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                HiddenFieldOrganizationId.Value = Request.QueryString["OrgID"].ToString();
            }
            AddressLookupControl1.SelectedValue += new EventHandler<Web.Controls.Lookup.SelectedValueEventArgs>(AddressLookupControl1_SelectedValue);
        }

        /// <summary>
        /// Select value event of addresslookupcontrol
        /// </summary>
        void AddressLookupControl1_SelectedValue(object sender, Controls.Lookup.SelectedValueEventArgs e)
        {
            HiddenFieldAddressId.Value = e.Address.AddressId.ToString();
            TextBoxPostcode.Text = e.Address.Postcode;
            TextBoxAddressLine1.Text = e.Address.AddressLine;
            TextBoxAddressLine2.Text = e.Address.AddressLine2;
            TextBoxAddressLine3.Text = e.Address.AddressLine3;
        }

        /// <summary>
        /// Load data
        /// </summary>
        void LoadData()
        {
            DirectorateView newDirectorate = new DirectorateBUS().GetDirectorate(Convert.ToInt32(Request.QueryString["directorateId"].ToString()));
            TextBoxDirectorateName.Text = newDirectorate.DirectorateName;
            TextBoxTypeOfBusiness.Text = newDirectorate.BusinessName;
            HiddenFieldBusinessId.Value = newDirectorate.BusinessId + "";
            TextBoxShortDescription.Text = newDirectorate.ShortDescription;
            HiddenFieldDirectorateId.Value = newDirectorate.DirectorateId + "";
            HiddenFieldOrganizationId.Value = newDirectorate.OrganizationId + "";
            TextBoxSicCode.Text = newDirectorate.SICCode;
            TextBoxLeadContact.Text = newDirectorate.FirstName + newDirectorate.Surname;
            HiddenFieldContactId.Value = newDirectorate.ContactId + "";
            TextBoxFullDescription.Text = newDirectorate.FullDescription;
            TextBoxAddressLine1.Text = newDirectorate.AddressLine;
            TextBoxAddressLine2.Text = newDirectorate.AddressLine2;
            TextBoxAddressLine3.Text = newDirectorate.AddressLine3;
            TextBoxPhoneNumber.Text = newDirectorate.PhoneNumber;
            TextBoxFax.Text = newDirectorate.Fax;
            TextBoxWebAddress.Text = newDirectorate.WebAddress;
            TextBoxPostcode.Text = newDirectorate.Postcode;
            TextBoxCharityNumber.Text = newDirectorate.CharityNumber;
            HiddenFieldAddressId.Value = newDirectorate.AddressId + "";
            TextBoxCityTown.Text = newDirectorate.CityTown;
            TextBoxCompanyNumber.Text = newDirectorate.CompanyNumber;
            TextBoxCounty.Text = newDirectorate.County;
            if (newDirectorate.CountryId == null)
            {
                DropDownListNationCountry.DataSource = CountryBO.SelectCountries();
                DropDownListNationCountry.DataTextField = "CountryName";
                DropDownListNationCountry.DataValueField = "CountryId";
                DropDownListNationCountry.DataBind();
            }
            else
            {
                DropDownListNationCountry.DataSource = CountryBO.SelectCountries();
                DropDownListNationCountry.DataTextField = "CountryName";
                DropDownListNationCountry.DataValueField = "CountryId";
                DropDownListNationCountry.DataBind();
                DropDownListNationCountry.Text = DropDownListNationCountry.Items.FindByValue(newDirectorate.CountryId).Text;
            }
            if (newDirectorate.IsActive == true)
            {
                LabelIsActive.Text = "Yes";
            }
            else
            {
                LabelIsActive.Text = "No";
            }
        }

        /// <summary>
        /// Update directorate
        /// </summary>
        private void AmendDirectorate()
        {
            SD.DataAccess.Directorate newDirectorate = new SD.DataAccess.Directorate();
            newDirectorate.DirectorateId = Convert.ToInt32(Request.QueryString["directorateId"].ToString());
            newDirectorate.DirectorateName = TextBoxDirectorateName.Text;
            if (!string.IsNullOrEmpty(HiddenFieldBusinessId.Value))
            {
                newDirectorate.BusinessId = Convert.ToInt32(HiddenFieldBusinessId.Value);
            }
            if (!string.IsNullOrEmpty(HiddenFieldAddressId.Value))
            {
                newDirectorate.AddressId = Convert.ToInt32(HiddenFieldAddressId.Value);
            }
            if (!string.IsNullOrEmpty(HiddenFieldContactId.Value))
            {
                newDirectorate.ContactId = Convert.ToInt32(HiddenFieldContactId.Value);
            }
            newDirectorate.ShortDescription = TextBoxShortDescription.Text;
            newDirectorate.FullDescription = TextBoxFullDescription.Text;
            newDirectorate.CityTown = TextBoxCityTown.Text;
            newDirectorate.County = TextBoxCounty.Text;
            newDirectorate.CountryId = DropDownListNationCountry.SelectedItem.Value;
            newDirectorate.PhoneNumber = TextBoxPhoneNumber.Text;
            newDirectorate.Fax = TextBoxFax.Text;
            newDirectorate.Email = TextBoxEmail.Text;
            newDirectorate.WebAddress = TextBoxWebAddress.Text;
            newDirectorate.CharityNumber = TextBoxCharityNumber.Text;
            newDirectorate.CompanyNumber = TextBoxCompanyNumber.Text;
            if (LabelIsActive.Text == "Yes")
            {
                newDirectorate.IsActive = true;
            }
            else
            {
                newDirectorate.IsActive = false;
            }
            if (!string.IsNullOrEmpty(HiddenFieldOrganizationId.Value))
            {
                newDirectorate.OrganizationId = Convert.ToInt32(HiddenFieldOrganizationId.Value);
            }
            bool success = new DirectorateBUS().AmendDirectorate(newDirectorate);
            //check update
            if (success == true)
            {
                LabelSuccess.Text = "Update successfully!";
            }
        }

        /// <summary>
        /// Save button click event
        /// </summary>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            DirectorateView newDirectorate = new DirectorateBUS().GetDirectorate(Convert.ToInt32(Request.QueryString["directorateId"].ToString()));
            //check new name != old name ?
            if (newDirectorate.DirectorateName != TextBoxDirectorateName.Text)
            {
                if (new DirectorateBUS().CheckNameDirectorate(TextBoxDirectorateName.Text))
                {
                    LabelNameExists.Text = "Directorate Name has already existed";
                }
                else
                {
                    AmendDirectorate();
                    LabelNameExists.Text = "";
                }
            }
            else
            {
                AmendDirectorate();
            }
        }

        /// <summary>
        /// Back button click event. Redirect to the directorate list page
        /// </summary>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Views/Orgranization/OrganizationDetail.aspx?id=" + HiddenFieldOrganizationId.Value);
        }

        /// <summary>
        /// TypeOfBusinessLookup button click event. Open TypeOfBusinessLookup
        /// </summary>
        protected void ButtonTypeOfBusinessLookup_Click(object sender, EventArgs e)
        {
            TypeOfBusinessLookup1.Popup();
        }

        /// <summary>
        /// ContactLookup button click event. Open ContactLookup
        /// </summary>
        protected void ButtonContactLookup_Click(object sender, EventArgs e)
        {
            ContactLookup1.Popup();
        }

        /// <summary>
        /// AddressLookup button click event. Open AddressLookupControl
        /// </summary>
        protected void ButtonPostcodeLookup_Click(object sender, EventArgs e)
        {
            AddressLookupControl1.Popup();
        }

        /// <summary>
        /// Deactivate button click event
        /// </summary>
        protected void ButtonDeactivate_Click(object sender, EventArgs e)
        {
            if (LabelIsActive.Text == "Yes")
            {
                bool check = new DirectorateBUS().ChangeDirectorateStatus(Convert.ToInt32(Request.QueryString["directorateID"].ToString()), false);
                if (check == true)
                {
                    LabelSuccess.Text = "Directorate is deactivated";
                    LabelIsActive.Text = "No";
                }
            }
            else
            {
                LabelSuccess.Text = "Directorate is deactivated";
            }
        }
    }
}